package com.hxzy.controller.admin.category;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.resp.PageResp;
import com.hxzy.common.vo.resp.R;
import com.hxzy.controller.admin.category.req.CategoryClassAddReq;
import com.hxzy.controller.admin.category.req.CategoryClassEditReq;
import com.hxzy.controller.admin.category.req.CategoryClassReq;
import com.hxzy.controller.admin.category.resp.CategoryClassResp;
import com.hxzy.controller.admin.login.resp.AdminLoginResp;
import com.hxzy.controller.admin.mapstruct.CategoryClassConvert;
import com.hxzy.entity.CategoryClass;
import com.hxzy.service.CategoryClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/7/12 10:51
 * @Version 1.0
 */
@Api(tags = "管理后台-商品分类列表")
@RestController
@RequestMapping(value = "/api/category")
public class CategoryClassController {

    @Autowired
    private CategoryClassService categoryClassService;


    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R<PageResp<CategoryClassResp>> search(CategoryClassReq req){
        //如果是商家，必须要赋值，只允许查询自己的数据
        AdminLoginResp adminLoginResp = WaimaiThreadLocal.loginVOThreadLocal.get();
        //如果是商家
        if(adminLoginResp.getType()==2){
            req.setMerchantId(adminLoginResp.getId());
        }

        PageInfo<CategoryClassResp> pageInfo=this.categoryClassService.search(req);

        PageResp<CategoryClassResp> resp=new PageResp<>();
        resp.setTotal(pageInfo.getTotal());
        resp.setRows(pageInfo.getList());
        return R.okHasData(resp);

    }


    @ApiOperation(value = "新增")
    @PostMapping
    public R  insert(@RequestBody @Valid CategoryClassAddReq req) {
        AdminLoginResp adminLoginResp = WaimaiThreadLocal.loginVOThreadLocal.get();

        CategoryClass entity = CategoryClassConvert.INSTANCE.convert(req);

        entity.setMerchantId(adminLoginResp.getId());

        //保存
        boolean result = this.categoryClassService.save(entity);
        return  result? R.ok(): R.build(AckCode.DATABASE_ERROR);
    }


    @ApiOperation(value = "修改")
    @PutMapping
    public R  update(@RequestBody @Valid CategoryClassEditReq   req) {
        AdminLoginResp adminLoginResp = WaimaiThreadLocal.loginVOThreadLocal.get();

        CategoryClass db=this.categoryClassService.getById(req.getId());
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        CategoryClass entity = CategoryClassConvert.INSTANCE.convert(req);

        //执行
        boolean result = this.categoryClassService.updateById(entity);

        return   result? R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R<CategoryClassResp> findById(@PathVariable(value = "id") Long id){
        CategoryClass db=this.categoryClassService.getById(id);
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(CategoryClassConvert.INSTANCE.convert(db));
    }




}
